﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace FigureDraw
{
    public class MoveSymbolDataFlow : MoveSymbol
    {
        public MoveSymbolDataFlow(StorageDevice sd)
            : base(sd)
        {
            this.strInfor = "";
        }

        public MoveSymbolDataFlow(StorageDevice sd, Infor infor)
            : base(sd, infor)
        {
            
        }

        public override void DrawToDevice(Form form)
        {
            this.R = 255;
            this.G = 0;
            this.B = 0;
            this.LineWidth = 5;
            this.KhoiTaoTruocKhiVe();


            this.StorageDevice.drawArrow(this.BeginPoint.X, this.BeginPoint.Y, this.EndPoint.X, this.EndPoint.Y);
            /*
            Point[] arrPoint;
            float radius;
            
            //O(X0, Y0) là tâm đường tròn
            //A(XA, YA), B(XB, YB) là 2 điểm đầu và cuối con chuột
            //I(XI, YI) trung điểm AB
            //y1 = ax + b: phương trình đường thẳng
            //y2 = -x/a + d = cx + d: phương trình đường thẳng vuông góc y1;
            //radius: Bán kính đường tròn = khoảng cách AB;
            Point A, B, O, I;
            A = this.BeginPoint;
            B = this.EndPoint;
            O = new Point();
            radius = (float)Math.Abs(Math.Sqrt((A.X - B.X) * (A.X - B.X) + (A.Y - B.Y) * (A.Y - B.Y)));
            I = new Point((int)((A.X + B.X) / 2), (int)((A.Y + B.Y) / 2));
            
            if (radius > 50)
            {

                float c = 0, d;
                if (A.Y - B.Y != 0)
                    c = ((float)(B.X - A.X) / (float)(A.Y - B.Y));
                d = I.Y - c * I.X;

                //ax2 + bx + CO = 0
                float a, b, CO, dt;
                a = (float)(1 + c*c);
                b = (float)(2) * (-A.X - c * A.Y + c * d);
                CO = (float)(A.X*A.X + A.Y*A.Y + d*d - radius*radius - 2*d*A.Y);

                dt = b * b - 4 * a * CO;

                O.X = (int)(((-b) + Math.Sqrt(dt)) / (2*a));
                O.Y = (int)(c*O.X + d);

                arrPoint = new Point[5];
                //float deta = (float)(72 * Math.PI / 180);
                float deta = (float)(Math.PI / 12);
                float angle = (float)Math.Asin((A.Y -O.Y)/radius);
                //if(angle < 0)
                //    angle = (float)Math.Asin((A.X - O.X) / radius);

                //arrPoint[0] = A;
                //arrPoint[4] = B;
                
                for (int i = 0; i < 5; i++)
                {
                    arrPoint[i].X = O.X + (int)(radius * Math.Cos((double)(angle)));
                    arrPoint[i].Y = O.Y + (int)(radius * Math.Sin((double)(angle)));

                    if (B.Y < A.Y)
                        angle -= deta;
                    else
                        angle += deta;
                }

                
                StorageDevice.drawEllipse(O.X, O.Y, (int)radius, (int)radius);

                this.StorageDevice.drawCurve(arrPoint);
            }
            */

            /*
            if (radius > 50)
            {
                

                float a = 0;
                a = ((float)(A.Y - B.Y) / (float)(A.X - B.X));
                float d = 0;
                d = I.Y + 1 / a * I.X;
                float dt = 0;
                dt = (-2 * A.X - 2 * d / a + 2 * A.Y / a) * (-2 * A.X - 2 * d / a + 2 * A.Y / a) + 4*(1 + 1/(a*a))*(radius*radius - A.X* A.X - d*d + 2*A.Y*d -A.Y*A.Y);

                O.X = (int)(((2 * A.X + 2 * d / a - 2 * A.Y / a) + Math.Sqrt(dt)) / (2 * (1 + 1 / (a * a))));
                O.Y = (int)(O.X * (-1) / a + d);

                arrPoint = new Point[5];
                float deta = (float)(Math.PI / 3);
                float angle = 0;

                //arrPoint[0] = A;
                //arrPoint[4] = B;

                for (int i = 0; i < 5; i++)
                {
                    arrPoint[i].X = O.Y + (int)(radius * Math.Cos((double)(angle)));
                    arrPoint[i].Y = O.X + (int)(radius * Math.Sin((double)(angle)));

                    angle += deta;
                }


                this.StorageDevice.drawCurve(arrPoint);
            }
            
            */

            
        }
    }
}
